    //Random Game Engine
    //Copyright (C) 2012  Nikolay Dionisov

    //This program is free software: you can redistribute it and/or modify
    //it under the terms of the GNU General Public License as published by
    //the Free Software Foundation, either version 3 of the License, or
    //(at your option) any later version.

    //This program is distributed in the hope that it will be useful,
    //but WITHOUT ANY WARRANTY; without even the implied warranty of
    //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    //GNU General Public License for more details.

    //You should have received a copy of the GNU General Public License
    //along with this program.  If not, see <http://www.gnu.org/licenses/>.

	//------------------------------------------------------------------------

	//FILE NAME: RandomEngine.cpp
	//Implementation of the main class of this engine/library.
	//Taking care of other system's initializing, lifespan and destruction.


#include "RandomEngine.h"


namespace re
{
	namespace core
	{
		BaseOSFramework<RandomEngine>* BaseOSFramework<RandomEngine>::g_framework = NULL;
		//--------------------------------------------------------------------------------------------------------
		//Functions for initializing and running the engine.
		//--------------------------------------------------------------------------------------------------------
		bool RandomEngine::Initialize()
		{
			if(!UpdateEngineProperties()) //this shoud be overwrited by the user, so he can set up starting properties, like window name, window size, etc
			{
				return false;
			}



#ifdef RE_ENABLE_CORE_WINDOWS
			osframework_ = OSFrameworkPtr(RE_NEW WindowsFramework<RandomEngine>);
#endif
			if(osframework_ == OSFrameworkPtr())
			{
				assert(0 && "Failed to create a working osframework. Fix ur code!");
				return false;
			}
			osframework_->SetCallbackCreateDevice(&RandomEngine::CreateDevice);
			osframework_->SetCallbackDestroyDevice(&RandomEngine::DestroyDevice);
			osframework_->SetCallbackLostDevice(&RandomEngine::LostDevice);
			osframework_->SetCallbackResetDevice(&RandomEngine::ResetDevice);
			osframework_->SetCallbackUpdate(&RandomEngine::Update);
			osframework_->SetCallbackRender(&RandomEngine::Render);

			osframework_->VInitialize(&settings_, RER_DIRECT3D9); // initialize the framework

			//create the logger
			{
			LoggerProcess* logger_p = new LoggerProcess;
			IThreadForwarder * forwarder = new ThreadForwarder<LoggerProcess>(logger_p, &LoggerProcess::MainLoop);
			osframework_->CreateThread(forwarder);
			pool_.AttachThread(RealtimeProcessPtr(logger_p));
			}

			//create the resource cache
			{
			io::ResourceCacheProcess* rescache_process = new io::ResourceCacheProcess;
			IThreadForwarder * forwarder = new ThreadForwarder<io::ResourceCacheProcess>(rescache_process, &io::ResourceCacheProcess::MainLoop);
			osframework_->CreateThread(forwarder);
			pool_.AttachThread(RealtimeProcessPtr(rescache_process));
			}


			GetEventManager()->VRegisterEvent(LoadFileRequest::eventtype_);
			GetEventManager()->VRegisterEvent(LoadFileResult::eventtype_);
			GetEventManager()->VTriggerEvent(EventDataPtr(new LoadFileRequest(std::wstring(L"W32TestProject.pdb"))));
			GetEventManager()->VTriggerEvent(EventDataPtr(new LoadFileRequest(std::wstring(L"RandomEngine.sdf"))));
			GetEventManager()->VTriggerEvent(EventDataPtr(new LoadFileRequest(std::wstring(L"RandomEngine2.sdf"))));
			return true;
		}
		int RandomEngine::RunEngine()
		{
			osframework_->VRun();
			return 0;
		}
		//--------------------------------------------------------------------------------------------------------
		//Private Functions, controlling the subsystems.
		//--------------------------------------------------------------------------------------------------------
		bool RandomEngine::ShutDown()
		{
			osframework_->VShutDown();
			return true;
		}
		bool RandomEngine::UpdateEngineProperties()
		{
			settings_.app_name = L"Random Engine Window";
			settings_.width = 640;
			settings_.height = 480;
			settings_.is_running = true;
			return true;
		}
		bool RandomEngine::CreateGameAndView()
		{
			return true;
		}
		bool RandomEngine::Update()
		{
			return true;
		}
		void RandomEngine::Render()
		{
		}
		void RandomEngine::CreateDevice(IRenderer * renderer)
		{
		}
		void RandomEngine::DestroyDevice()
		{
		}
		void RandomEngine::LostDevice()
		{
		}
		void RandomEngine::ResetDevice()
		{
		}
	}
}